package com.itheima.service.impl;


import com.itheima.common.AuthThreadLocal;
import com.itheima.common.JwtToken;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.dto.shoppingCartDto.ShoppingCartAddDto;
import com.itheima.pojo.dto.shoppingCartDto.ShoppingCartDeleteDto;
import com.itheima.pojo.dto.shoppingCartDto.ShoppingCartUpdateDto;
import com.itheima.pojo.vo.shoppingCartVo.GetShoppingCartVo;
import com.itheima.service.ShoppingCartService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author tcotk
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public boolean add(ShoppingCartAddDto shoppingCart) {
        List<GetShoppingCartVo> list = shoppingCartMapper.selectAll();
        if (list.size() == 0) {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(JwtToken.parse(AuthThreadLocal.getAmount()));
            return shoppingCartMapper.add(shoppingCart) > 0;
        } else {
            for (GetShoppingCartVo cart : list) {
                boolean result1 = !(cart.getDishId()!=null
                        &&cart.getDishId().equals(shoppingCart.getDishId())
                        && shoppingCart.getSetmealId() == null);
                boolean result2 = !(cart.getSetmealId()!=null
                        &&cart.getSetmealId().equals(shoppingCart.getSetmealId())
                        && shoppingCart.getDishId() == null);

                if ((result1 && result2)) {
                    shoppingCart.setCreateTime(LocalDateTime.now());
                    shoppingCart.setUserId(JwtToken.parse(AuthThreadLocal.getAmount()));
                    shoppingCart.setNumber(1);
                    return shoppingCartMapper.add(shoppingCart) > 0;
                } else {
                    ShoppingCartUpdateDto shoppingCartUpdateDto=new ShoppingCartUpdateDto();


                    shoppingCart.setNumber(cart.getNumber() + 1);
                    BeanUtils.copyProperties(shoppingCart,shoppingCartUpdateDto );
                    return shoppingCartMapper.update(shoppingCartUpdateDto) > 0;


                }


            }
        }
        return false;

    }

    @Override
    public boolean clean() {

        return shoppingCartMapper.clean() > 0;
    }

    @Override
    public boolean deleteByDishId(ShoppingCartDeleteDto shoppingCart) {
        String dishId = shoppingCart.getDishId();
        String setmealId = shoppingCart.getSetmealId();
        for (GetShoppingCartVo cart : shoppingCartMapper.selectAll()) {
            if (cart.getNumber() <= 1) {

                if (setmealId == null && dishId.equals(cart.getDishId())) {

                    return shoppingCartMapper.deleteDishId(dishId) > 0;
                } else if (cart.getDishId() == null && setmealId.equals(cart.getSetmealId())) {
                    return shoppingCartMapper.deleteSetmealId(setmealId) > 0;
                }
            } else {
                cart.setNumber(cart.getNumber() - 1);
                ShoppingCartUpdateDto shoppingCartUpdateDto=new ShoppingCartUpdateDto();

                BeanUtils.copyProperties(cart,shoppingCartUpdateDto );
                return shoppingCartMapper.update(shoppingCartUpdateDto) > 0;
            }
        }
        return false;

    }

    @Override
    public List<GetShoppingCartVo> selectAll() {

        return shoppingCartMapper.selectAll();
    }
}
